
#ifndef INCLUDE_IO_EPOLL
#define INCLUDE_IO_EPOLL

#include "io_reuse_module_impl.h"

#include <cstdint>
#include <map>
#include <memory>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>

class IoEpoll : public std::enable_shared_from_this<IoEpoll>,
                public IoReuseModuleImpl
{
public:
    IoEpoll(int size = 60000);
    virtual ~IoEpoll() override;
    int AddEpollEvent(int description, struct epoll_event & event) const;
    int ModEpollEvent(int description, struct epoll_event & event) const;
    int DelEpollEvent(int description) const;
    int EpollWait(std::vector<struct epoll_event> & epoll_event_vec, int wait_time = -1) const;

public:
    int64_t AddEvent(int description, int event) override;
    int64_t ModEvent(int64_t id, int event) override;
    int64_t DelEvent(int64_t id) override;
    int     WaitEvent(std::vector<struct EventData> & trigger_data, int wait_time) override;

private:
    int                    m_epoll_fd;
    std::map<int64_t, int> m_socket_map;
    int64_t                m_index;
};

class IoEpollFactory : public IoReuseModuleFactory
{
public:
    ~IoEpollFactory() override = default;
    std::shared_ptr<IoReuseModuleImpl> CreateModule() override;
};

#endif
